package ifest.ontology;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.obeonetwork.dsl.spem.Default_TaskDefinitionParameter;
import org.obeonetwork.dsl.spem.ParameterDirectionKind;
import org.obeonetwork.dsl.spem.WorkDefinitionParameter;
import org.obeonetwork.dsl.spem.extension.spem_ifest.Artifact;
import org.obeonetwork.dsl.spem.extension.spem_ifest.ArtifactDefinition;
import org.obeonetwork.dsl.spem.extension.spem_ifest.EngineeringDomain;
import org.obeonetwork.dsl.spem.extension.spem_ifest.Level;
import org.obeonetwork.dsl.spem.extension.spem_ifest.TaskDefinition;
import org.obeonetwork.dsl.spem.extension.spem_ifest.ToolType;
import org.obeonetwork.dsl.spem.extension.spem_ifest.Viewpoint;
import org.obeonetwork.dsl.spem.extension.spem_ifest.impl.Spem_ifestFactoryImpl;
import org.obeonetwork.dsl.spem.impl.SpemFactoryImpl;
import org.semanticweb.owlapi.apibinding.OWLManager;
import org.semanticweb.owlapi.model.IRI;
import org.semanticweb.owlapi.model.MissingImportEvent;
import org.semanticweb.owlapi.model.MissingImportListener;
import org.semanticweb.owlapi.model.OWLClass;
import org.semanticweb.owlapi.model.OWLClassAssertionAxiom;
import org.semanticweb.owlapi.model.OWLDataFactory;
import org.semanticweb.owlapi.model.OWLIndividual;
import org.semanticweb.owlapi.model.OWLLiteral;
import org.semanticweb.owlapi.model.OWLNamedIndividual;
import org.semanticweb.owlapi.model.OWLObjectProperty;
import org.semanticweb.owlapi.model.OWLObjectPropertyAssertionAxiom;
import org.semanticweb.owlapi.model.OWLOntology;
import org.semanticweb.owlapi.model.OWLOntologyCreationException;
import org.semanticweb.owlapi.model.OWLOntologyManager;
import org.semanticweb.owlapi.util.SimpleIRIMapper;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

public class SPEMOntologyFactory {
	private OWLOntologyManager manager;
	private OWLOntology ont;
	private IFEST_Class_Mapping mapping;
	private Spem_ifestFactoryImpl ifestFactory;
	private String tempFileName = "D://Working/ontologies/temp.owl";
	private String sqwlrFile = "D://Working/ontologies/lib/sqwrl.owl";
	private String swlraFile = "D://Working/ontologies/lib/swrla.owl";
	private String server = "";
	private String context = "";
	private String repoID = "";

	public void init(String repositorySettingXMLFile, String ifestMappingFile)
			throws SAXException, IOException, ParserConfigurationException,
			OWLOntologyCreationException {
		mapping = new IFEST_Class_Mapping(ifestMappingFile);
		ifestFactory = new Spem_ifestFactoryImpl();
		DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
		factory.setValidating(true);
		factory.setExpandEntityReferences(false);
		Document doc = factory.newDocumentBuilder().parse(
				new File(repositorySettingXMLFile));
		Node root = doc.getDocumentElement();
		Node config = doc.getElementsByTagName("config").item(0);
		NodeList list = config.getChildNodes();
		for (int i = 0; i < list.getLength(); i++) {
			if (list.item(i).getNodeType() != Node.ELEMENT_NODE)
				continue;
			Element e = (Element) list.item(i);
			if (e.getNodeName().equalsIgnoreCase("server")) {
				server = e.getTextContent();
			}
			if (e.getNodeName().equalsIgnoreCase("repositoryID")) {
				repoID = e.getTextContent();
			}
			if (e.getNodeName().equalsIgnoreCase("context")) {
				context = e.getTextContent();
			}
		}
		manager = OWLManager.createOWLOntologyManager();
		IRI iri = IRI
				.create("http://sqwrl.stanford.edu/ontologies/built-ins/3.4/sqwrl.owl");
		File file = new File(sqwlrFile);
		manager.addIRIMapper(new SimpleIRIMapper(iri, IRI.create(file)));
		iri = IRI.create("http://swrl.stanford.edu/ontologies/3.3/swrla.owl");
		file = new File(swlraFile);
		manager.addIRIMapper(new SimpleIRIMapper(iri, IRI.create(file)));
		OWLIMTool.getOWL("http://www.spem-ifest.org/ontology/metamodel.owl",
				"D://Working/ontologies/temp_ifest.owl");
		manager.addMissingImportListener(new MissingImportListener() {
			
			public void importMissing(MissingImportEvent arg0) {
				// TODO Auto-generated method stub
				return;
			}
		});
		manager.loadOntologyFromOntologyDocument(new File(
						"D://Working/ontologies/temp_ifest.owl"));
		OWLIMTool.getOWL("http://www.spem-ifest.org",
				"D://Working/ontologies/temp_ifest.owl");
		manager.loadOntologyFromOntologyDocument(new File(
						"D://Working/ontologies/temp_ifest.owl"));
	}
	public void getOntologyContent(String uri)
	{
		OWLIMTool.getOWL(uri, tempFileName);
		File file = new File(tempFileName);
		try {
			manager.setSilentMissingImportsHandling(true);
			manager.addMissingImportListener(new MissingImportListener() {
				
				public void importMissing(MissingImportEvent arg0) {
					// TODO Auto-generated method stub
					System.out.println("###MANAGER### : import of "
							+ arg0.getImportedOntologyURI() + " FAILED");
					return;
				}
			});
			ont = manager.loadOntologyFromOntologyDocument(file);
		} catch (OWLOntologyCreationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		System.out.println("load ontology: " + ont.getOntologyID());
	}
	public List<EngineeringDomain> getEngineeringDomain()
			throws OWLOntologyCreationException {
		List<EngineeringDomain> list = new ArrayList<EngineeringDomain>();
		Spem_ifestFactoryImpl ifestfactory = new Spem_ifestFactoryImpl();
		OWLDataFactory factory = manager.getOWLDataFactory();
		OWLClass clsEngineeringDomain = factory.getOWLClass(IRI
				.create(mapping.ENGINEERING_DOMAIN));
		OWLClass clsViewpoint = factory.getOWLClass(IRI
				.create(mapping.VIEWPOINT));
		// get Engineering Domain Individuals
		Set<OWLClassAssertionAxiom> individuals = ont
				.getClassAssertionAxioms(clsEngineeringDomain);
		for (OWLClassAssertionAxiom i : individuals) {
			OWLNamedIndividual instance = (OWLNamedIndividual) i
					.getIndividual();
			EngineeringDomain e = ifestfactory.createEngineeringDomain();
			e.setName(getName(instance));
			e.setUri(instance.toStringID());
			e.getViewpoints().addAll(getViewPoints(instance));

			list.add(e);
		}
		return list;
	}

	protected String getName(OWLNamedIndividual indivi) {
		String name = "";
		Set<OWLLiteral> values = indivi.getDataPropertyValues(
				manager.getOWLDataFactory().getOWLDataProperty(
						IRI.create(mapping.HAVE_NAME_VALUE)), ont);
		if (values.size() > 0)
			name = values.iterator().next().getLiteral();
		return name;
	}

	protected List<Viewpoint> getViewPoints(OWLNamedIndividual indivi) {
		List<Viewpoint> list = new ArrayList<Viewpoint>();
		OWLDataFactory factory = manager.getOWLDataFactory();
		OWLObjectProperty containsViewpoint = factory.getOWLObjectProperty(IRI
				.create(mapping.HAVE_VIEWPOINT));
		Set<OWLObjectPropertyAssertionAxiom> objectproperties = ont
				.getObjectPropertyAssertionAxioms(indivi);
		for (OWLObjectPropertyAssertionAxiom o : objectproperties) {
			if (o.getProperty() == containsViewpoint) {
				// get all contained viewpoints
				Set<OWLClassAssertionAxiom> classAssViewpoints = ont
						.getClassAssertionAxioms(o.getObject());
				for (OWLClassAssertionAxiom v : classAssViewpoints) {
					OWLNamedIndividual owlViewpoint = (OWLNamedIndividual) v
							.getIndividual();
					Viewpoint viewpoint = ifestFactory.createViewpoint();
					viewpoint.setName(getName(owlViewpoint));
					viewpoint.setUri(owlViewpoint.toStringID());
					list.add(viewpoint);
				}

			}
		}
		return list;
	}

	public List<TaskDefinition> getTaskDefinitions(
			List<EngineeringDomain> engineeringDomains) {
		List<TaskDefinition> list = new ArrayList<TaskDefinition>();
	

		OWLDataFactory factory = manager.getOWLDataFactory();
		OWLClass clsActivity = factory.getOWLClass(IRI
				.create(mapping.DOMAIN_ACTIVITY));
		Set<OWLClassAssertionAxiom> individuals = ont
				.getClassAssertionAxioms(clsActivity);
		for (OWLClassAssertionAxiom i : individuals) {
			OWLNamedIndividual instance = (OWLNamedIndividual) i
					.getIndividual();
			TaskDefinition e = ifestFactory.createTaskDefinition();
			e.setName(getName(instance));
			e.setUri(instance.toStringID());
			OWLObjectProperty op_apply = factory.getOWLObjectProperty(IRI
					.create(mapping.PROCESSACTIVITY_APPLIES));
			Set<OWLIndividual> instances = instance
					.getObjectPropertyValues(op_apply, ont);
			List<String> viewpoints = new ArrayList<String>();
			for (OWLIndividual o : instances) {

				String viewpoint_uri = ((OWLNamedIndividual) o)
						.toStringID();
				viewpoints.add(viewpoint_uri);
			}
			for (EngineeringDomain domain : engineeringDomains) {
				for (Viewpoint viewpoint : domain.getViewpoints()) {
					for (String uri : viewpoints) {
						if (viewpoint.getUri().equalsIgnoreCase(uri)) {
							e.getApplies().add(viewpoint);
							if (!domain.getPerforms().contains(e)) {
								domain.getPerforms().add(e);
							}

						}
					}

				}
			}
			list.add(e);
		}
		 clsActivity = factory.getOWLClass(IRI
					.create(mapping.METHOD_ACTIVITY));
		 individuals = ont
					.getClassAssertionAxioms(clsActivity);
			for (OWLClassAssertionAxiom i : individuals) {
				OWLNamedIndividual instance = (OWLNamedIndividual) i
						.getIndividual();
				TaskDefinition e = ifestFactory.createTaskDefinition();
				e.setName(getName(instance));
				e.setUri(instance.toStringID());
				OWLObjectProperty op_apply = factory.getOWLObjectProperty(IRI
						.create(mapping.PROCESSACTIVITY_APPLIES));
				Set<OWLIndividual> instances = instance
						.getObjectPropertyValues(op_apply, ont);
				List<String> viewpoints = new ArrayList<String>();
				for (OWLIndividual o : instances) {

					String viewpoint_uri = ((OWLNamedIndividual) o)
							.toStringID();
					viewpoints.add(viewpoint_uri);
				}
				for (EngineeringDomain domain : engineeringDomains) {
					for (Viewpoint viewpoint : domain.getViewpoints()) {
						for (String uri : viewpoints) {
							if (viewpoint.getUri().equalsIgnoreCase(uri)) {
								e.getApplies().add(viewpoint);
								if (!domain.getPerforms().contains(e)) {
									domain.getPerforms().add(e);
								}

							}
						}

					}
				}
				list.add(e);
			}
		
		return list;

	}

	public List<ToolType> getToolTypes(
			List<EngineeringDomain> engineeringDomains) {
		List<ToolType> list = new ArrayList<ToolType>();
	
		OWLDataFactory factory = manager.getOWLDataFactory();
		OWLClass clssToolType = factory.getOWLClass(IRI
				.create(mapping.TOOLTYPE));
		Set<OWLClassAssertionAxiom> instances = ont
				.getClassAssertionAxioms(clssToolType);
		for (OWLClassAssertionAxiom i : instances) {
			OWLNamedIndividual instance = (OWLNamedIndividual) i
					.getIndividual();
			ToolType element = ifestFactory.createToolType();
			element.setName(getName(instance));
			element.setUri(instance.toStringID());
			OWLObjectProperty op_supports = factory
					.getOWLObjectProperty(IRI
							.create(mapping.TOOLTYPE_SUPPORTS));
			Set<OWLIndividual> viewpoints = instance
					.getObjectPropertyValues(op_supports,ont);
			List<String> viewpointsURIs = new ArrayList<String>();
			for (OWLIndividual v : viewpoints) {
				viewpointsURIs.add(v.toStringID());
			}
			for (EngineeringDomain domain : engineeringDomains) {
				for (Viewpoint viewpoint : domain.getViewpoints()) {
					for (String uri : viewpointsURIs) {
						if (viewpoint.getUri().equalsIgnoreCase(uri)) {
							element.getSupports().add(viewpoint);
							if (!domain.getRelies().contains(element)) {
								domain.getRelies().add(element);
							}
							viewpoints.remove(uri);
						}
					}

				}

			}
			list.add(element);
		}
		return list;

	}

	public List<TaskDefinition> getTaskDefinitions(
			List<EngineeringDomain> engineeringDomains, List<Artifact> artifacts) {
		List<TaskDefinition> list = new ArrayList<TaskDefinition>();
		

		OWLDataFactory factory = manager.getOWLDataFactory();
		OWLClass clsActivity = factory.getOWLClass(IRI
				.create(mapping.DOMAIN_ACTIVITY));
		Set<OWLClassAssertionAxiom> individuals = ont
				.getClassAssertionAxioms(clsActivity);
		for (OWLClassAssertionAxiom i : individuals) {
			OWLNamedIndividual instance = (OWLNamedIndividual) i
					.getIndividual();
			TaskDefinition e = ifestFactory.createTaskDefinition();
			e.setName(getName(instance));
			e.setUri(instance.toStringID());
			//Set applies viewpoint property
			OWLObjectProperty op_apply = factory.getOWLObjectProperty(IRI
					.create(mapping.PROCESSACTIVITY_APPLIES));
			Set<OWLIndividual> instances = instance
					.getObjectPropertyValues(op_apply,ont);
			List<String> viewpoints = new ArrayList<String>();
			for (OWLIndividual o : instances) {

				String viewpoint_uri = ((OWLNamedIndividual) o)
						.toStringID();
				viewpoints.add(viewpoint_uri);
			}
			for (EngineeringDomain domain : engineeringDomains) {
				for (Viewpoint viewpoint : domain.getViewpoints()) {
					for (String uri : viewpoints) {
						if (viewpoint.getUri().equalsIgnoreCase(uri)) {
							e.getApplies().add(viewpoint);
							if (!domain.getPerforms().contains(e)) {
								domain.getPerforms().add(e);
							}

						}
					}

				}
			}
			//Set in, out artifact property
			OWLObjectProperty op_in = factory.getOWLObjectProperty(IRI
					.create(mapping.PROCESSACTIVITY_CONSUMS));
			SpemFactoryImpl spemFactory = new SpemFactoryImpl();
			 instances = instance
					.getObjectPropertyValues(op_in,ont);
			for (OWLIndividual o : instances) {

				for (Artifact a:artifacts)
				{
					if (a.getUri().equalsIgnoreCase(o.toStringID()))
					{
						Default_TaskDefinitionParameter inproduct = spemFactory.createDefault_TaskDefinitionParameter();
					    inproduct.setDirection(ParameterDirectionKind.IN);
					    inproduct.setParameterType(a);
						e.getOwnedTaskDefinitionParameter().add(inproduct);		
						break;
					}
				}
			}
			OWLObjectProperty op_out = factory.getOWLObjectProperty(IRI
					.create(mapping.PROCESSACTIVITY_PRODUCES));
			
			 instances = instance
					.getObjectPropertyValues(op_out,ont);
			for (OWLIndividual o : instances) {

				for (Artifact a:artifacts)
				{
					if (a.getUri().equalsIgnoreCase(o.toStringID()))
					{
						Default_TaskDefinitionParameter inproduct = spemFactory.createDefault_TaskDefinitionParameter();
					    inproduct.setDirection(ParameterDirectionKind.OUT);
					    inproduct.setParameterType(a);
						e.getOwnedTaskDefinitionParameter().add(inproduct);		
						break;
					}
				}
			}
			list.add(e);
		}
	clsActivity = factory.getOWLClass(IRI
				.create(mapping.METHOD_ACTIVITY));
	individuals = ont
				.getClassAssertionAxioms(clsActivity);
		for (OWLClassAssertionAxiom i : individuals) {
			OWLNamedIndividual instance = (OWLNamedIndividual) i
					.getIndividual();
			TaskDefinition e = ifestFactory.createTaskDefinition();
			e.setName(getName(instance));
			e.setUri(instance.toStringID());
			//Set applies viewpoint property
			OWLObjectProperty op_apply = factory.getOWLObjectProperty(IRI
					.create(mapping.PROCESSACTIVITY_APPLIES));
			Set<OWLIndividual> instances = instance
					.getObjectPropertyValues(op_apply,ont);
			List<String> viewpoints = new ArrayList<String>();
			for (OWLIndividual o : instances) {

				String viewpoint_uri = ((OWLNamedIndividual) o)
						.toStringID();
				viewpoints.add(viewpoint_uri);
			}
			for (EngineeringDomain domain : engineeringDomains) {
				for (Viewpoint viewpoint : domain.getViewpoints()) {
					for (String uri : viewpoints) {
						if (viewpoint.getUri().equalsIgnoreCase(uri)) {
							e.getApplies().add(viewpoint);
							if (!domain.getPerforms().contains(e)) {
								domain.getPerforms().add(e);
							}

						}
					}

				}
			}
			//Set in, out artifact property
			OWLObjectProperty op_in = factory.getOWLObjectProperty(IRI
					.create(mapping.PROCESSACTIVITY_CONSUMS));
			SpemFactoryImpl spemFactory = new SpemFactoryImpl();
			 instances = instance
					.getObjectPropertyValues(op_in,ont);
			for (OWLIndividual o : instances) {

				for (Artifact a:artifacts)
				{
					if (a.getUri().equalsIgnoreCase(o.toStringID()))
					{
						Default_TaskDefinitionParameter inproduct = spemFactory.createDefault_TaskDefinitionParameter();
					    inproduct.setDirection(ParameterDirectionKind.IN);
					    inproduct.setParameterType(a);
						e.getOwnedTaskDefinitionParameter().add(inproduct);
						break;
					}
				}
			}
			OWLObjectProperty op_out = factory.getOWLObjectProperty(IRI
					.create(mapping.PROCESSACTIVITY_PRODUCES));
			
			 instances = instance
					.getObjectPropertyValues(op_out,ont);
			for (OWLIndividual o : instances) {

				for (Artifact a:artifacts)
				{
					if (a.getUri().equalsIgnoreCase(o.toStringID()))
					{
						Default_TaskDefinitionParameter inproduct = spemFactory.createDefault_TaskDefinitionParameter();
					    inproduct.setDirection(ParameterDirectionKind.OUT);
					    inproduct.setParameterType(a);
						e.getOwnedTaskDefinitionParameter().add(inproduct);		
						break;
					}
				}
			}
			list.add(e);
		}
		return list;

	}

	public List<ToolType> getToolTypes(String context,
			List<EngineeringDomain> engineeringDomains) {
		List<ToolType> list = new ArrayList<ToolType>();
	
		OWLDataFactory factory = manager.getOWLDataFactory();
		OWLClass clssToolType = factory.getOWLClass(IRI
				.create(mapping.TOOLTYPE));
		Set<OWLClassAssertionAxiom> instances = ont
				.getClassAssertionAxioms(clssToolType);
		for (OWLClassAssertionAxiom i : instances) {
			OWLNamedIndividual instance = (OWLNamedIndividual) i
					.getIndividual();
			ToolType element = ifestFactory.createToolType();
			element.setName(getName(instance));
			element.setUri(instance.toStringID());
			OWLObjectProperty op_supports = factory
					.getOWLObjectProperty(IRI
							.create(mapping.TOOLTYPE_SUPPORTS));
			Set<OWLIndividual> viewpoints = instance
					.getObjectPropertyValues(op_supports, ont);
			List<String> viewpointsURIs = new ArrayList<String>();
			for (OWLIndividual v : viewpoints) {
				viewpointsURIs.add(v.toStringID());
			}
			for (EngineeringDomain domain : engineeringDomains) {
				for (Viewpoint viewpoint : domain.getViewpoints()) {
					for (String uri : viewpointsURIs) {
						if (viewpoint.getUri().equalsIgnoreCase(uri)) {
							element.getSupports().add(viewpoint);
							if (!domain.getRelies().contains(element)) {
								domain.getRelies().add(element);
							}
						//viewpoints.remove(uri);
						}
					}

				}

			}
			list.add(element);
		}
		
		return list;

	}
public List<ArtifactDefinition> getArtifactDefinition( List<EngineeringDomain> engineeringDomains)
{
	List<ArtifactDefinition> rs= new ArrayList<ArtifactDefinition>();
	rs.addAll(getDomainArtifactDefinition(engineeringDomains));
	rs.addAll(getMethodArtifactDefinition(engineeringDomains,rs));
	return rs;
}
public List<ArtifactDefinition> getDomainArtifactDefinition(List<EngineeringDomain> engineeringDomains)
{
	List<ArtifactDefinition> rs= new ArrayList<ArtifactDefinition>();
	
	OWLDataFactory factory = manager.getOWLDataFactory();
	OWLClass owlArtifactDefinition = factory.getOWLClass(IRI
			.create(mapping.DOMAIN_ARTIFACT_DEFINITION));
	Set<OWLClassAssertionAxiom> instances = ont
			.getClassAssertionAxioms(owlArtifactDefinition);
	for (OWLClassAssertionAxiom i : instances) {
		OWLNamedIndividual instance = (OWLNamedIndividual) i
				.getIndividual();
		ArtifactDefinition element = ifestFactory.createArtifactDefinition();
		element.setName(getName(instance));
		element.setUri(instance.toStringID());
		element.setLevel(Level.DOMAIN);
		OWLObjectProperty op_categorized = factory
				.getOWLObjectProperty(IRI
						.create(mapping.ARTIFACTDEFINTITION_CATEGORIZED));
		Set<OWLIndividual> viewpoints = instance
				.getObjectPropertyValues(op_categorized, ont);
		List<String> viewpointsURIs = new ArrayList<String>();
		for (OWLIndividual v : viewpoints) {
			viewpointsURIs.add(v.toStringID());
		}
		for (EngineeringDomain domain : engineeringDomains) {
			for (Viewpoint viewpoint : domain.getViewpoints()) {
				for (String uri : viewpointsURIs) {
					if (viewpoint.getUri().equalsIgnoreCase(uri)) {
						element.getOrganized().add(viewpoint);
						
					//	viewpoints.remove(uri);
					}
				}

			}

		}
		rs.add(element);
	}
	
	return rs;
}
public List<ArtifactDefinition> getMethodArtifactDefinition(List<EngineeringDomain> engineeringDomains,List<ArtifactDefinition> domainArtifactDefinitions)
{
	List<ArtifactDefinition> rs= new ArrayList<ArtifactDefinition>();
	
	OWLDataFactory factory = manager.getOWLDataFactory();
	OWLClass owlArtifactDefinition = factory.getOWLClass(IRI
			.create(mapping.METHOD_ARTIFACT_DEFINITION));
	Set<OWLClassAssertionAxiom> instances = ont
			.getClassAssertionAxioms(owlArtifactDefinition);
	for (OWLClassAssertionAxiom i : instances) {
		OWLNamedIndividual instance = (OWLNamedIndividual) i
				.getIndividual();
		ArtifactDefinition element = ifestFactory.createArtifactDefinition();
		element.setName(getName(instance));
		element.setUri(instance.toStringID());
		element.setLevel(Level.METHOD);
		//Set specialized properties
		OWLObjectProperty op_specialized = factory.getOWLObjectProperty(IRI.create(mapping.METHODARTIFACT_SPECIALIZED));
		Set<OWLIndividual> domainADs = instance.getObjectPropertyValues(op_specialized, ont);
		List<String> domainADUris = new ArrayList<String>();
		for (OWLIndividual a:domainADs)
		{
			domainADUris.add(a.toStringID());
		}
		//Set categorized properties
		OWLObjectProperty op_categorized = factory
				.getOWLObjectProperty(IRI
						.create(mapping.ARTIFACTDEFINTITION_CATEGORIZED));
		Set<OWLIndividual> viewpoints = instance
				.getObjectPropertyValues(op_categorized, ont);
		List<String> viewpointsURIs = new ArrayList<String>();
		for (OWLIndividual v : viewpoints) {
			viewpointsURIs.add(v.toStringID());
		}
		for (EngineeringDomain domain : engineeringDomains) {
			for (Viewpoint viewpoint : domain.getViewpoints()) {
				for (String uri : viewpointsURIs) {
					if (viewpoint.getUri().equalsIgnoreCase(uri)) {
						element.getOrganized().add(viewpoint);
						
					//	viewpoints.remove(uri);
					}
				}

			}

		}
		
		rs.add(element);
	}
	
	return rs;
}
public List<Artifact> getArtifact(List<ArtifactDefinition> artdefinitions)
{
	List<Artifact> rs = new ArrayList<Artifact>();
	rs.addAll(getDomainArtifact(artdefinitions));
	rs.addAll(getMethodArtifact(artdefinitions));
	return rs;
}
public List<Artifact> getDomainArtifact( List<ArtifactDefinition> artdefinitions)
{
List<Artifact> rs = new ArrayList<Artifact>();
OWLDataFactory factory = manager.getOWLDataFactory();
OWLClass owlArtifactDefinition = factory.getOWLClass(IRI
		.create(mapping.DOMAIN_ARTIFACT));
Set<OWLClassAssertionAxiom> instances = ont
		.getClassAssertionAxioms(owlArtifactDefinition);
for (OWLClassAssertionAxiom i : instances) {
	OWLNamedIndividual instance = (OWLNamedIndividual) i
			.getIndividual();
	Artifact element = ifestFactory.createArtifact();
	element.setName(getName(instance));
	element.setUri(instance.toStringID());
	//Set relation to ArtifactDefinition
	OWLObjectProperty op_conforms = factory.getOWLObjectProperty(IRI.create(mapping.ARTIFACT_CONFORMS));
	Set<OWLIndividual> owlinArtifactDefinitions = instance.getObjectPropertyValues(op_conforms, ont);
	for (OWLIndividual j:owlinArtifactDefinitions)
	{
		
		for (ArtifactDefinition a: artdefinitions)
		{
			if (a.getUri().equalsIgnoreCase(j.toStringID()))
			{
				element.getGuidance().add(a);
				break;
			}
		}
	}
	
	
	rs.add(element);
}

	return rs;
}
public List<Artifact> getMethodArtifact(List<ArtifactDefinition> artdefinitions)
{
List<Artifact> rs = new ArrayList<Artifact>();
OWLDataFactory factory = manager.getOWLDataFactory();
OWLClass owlArtifactDefinition = factory.getOWLClass(IRI
		.create(mapping.METHOD_ARTIFACT));
Set<OWLClassAssertionAxiom> instances = ont
		.getClassAssertionAxioms(owlArtifactDefinition);
for (OWLClassAssertionAxiom i : instances) {
	OWLNamedIndividual instance = (OWLNamedIndividual) i
			.getIndividual();
	Artifact element = ifestFactory.createArtifact();
	element.setName(getName(instance));
	element.setUri(instance.toStringID());
	//Set relation to ArtifactDefinition
	OWLObjectProperty op_conforms = factory.getOWLObjectProperty(IRI.create(mapping.ARTIFACT_CONFORMS));
	Set<OWLIndividual> owlinArtifactDefinitions = instance.getObjectPropertyValues(op_conforms, ont);
	for (OWLIndividual j:owlinArtifactDefinitions)
	{
		
		for (ArtifactDefinition a: artdefinitions)
		{
			if (a.getUri().equalsIgnoreCase(j.toStringID()))
			{
				element.getGuidance().add(a);
				break;
			}
		}
	}
	
	
	rs.add(element);
}
	return rs;
}
}
